package com.py.credit.service.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.py.credit.common.XypjConstants;
import com.py.credit.dao.SysDictDao;
import com.py.credit.entity.DictDetailEntity;
import com.py.credit.entity.DictEntity;
import com.py.credit.service.SysDictService;
import com.py.framework.core.common.Page;
import com.py.framework.core.exception.ServiceException;

/**
 * 字典服务Service实现
 *
 * @author Leegern
 * @date 2018年4月20日
 */
@Service
public class SysDictServiceImpl implements SysDictService {

	@Autowired
	private SysDictDao sysDictDao;

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addDict(DictEntity entity) {
		entity.setDelFlag(XypjConstants.NUMBER_ONE);
		entity.setEnabled(XypjConstants.NUMBER_TWO);
		return sysDictDao.addDict(entity);
	}

	@Override
	public List<DictEntity> queryDictListForPage(String dictKey, String dictName, String enabled, String dtlValue,
			Page page) {
		return sysDictDao.queryDictListForPage(dictName, dictKey, enabled, dtlValue, page);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public DictEntity fineOneDict(long id) {
		if (id > XypjConstants.NUMBER_ZERO) {
			return sysDictDao.fineOneDict(id);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.SysDictService#batchUpdateDictStatus(java.util.
	 * List, int, int)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int batchUpdateDictStatus(List<Long> ids, int enabled, int delFlag) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!CollectionUtils.isEmpty(ids)) {
			params.put("ids", ids);
		}
		if (enabled > XypjConstants.NUMBER_ZERO) {
			params.put("enabled", enabled);
		}
		if (delFlag > XypjConstants.NUMBER_ZERO) {
			DictEntity detail = null;
			for (Long id : ids) {
				// 判断是否使用中
				detail = this.fineOneDict(id);
				if (null != detail && detail.getEnabled() == XypjConstants.NUMBER_TWO) {
					throw new ServiceException(detail.getDictName() + "正在启用中，删除失败！");
				}
			}
			params.put("delFlag", delFlag);
		}
		return sysDictDao.batchUpdateDictStatus(params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.py.framework.service.SysDictService#updateDict(com.py.framework.
	 * entity.DictEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int updateDict(DictEntity entity) {
		return sysDictDao.updateDict(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.SysDictService#addDictDetail(com.py.framework.
	 * entity.DictDetailEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int addDictDetail(DictDetailEntity entity) {
		entity.setDelFlag(XypjConstants.NUMBER_ONE);
		entity.setEnabled(XypjConstants.NUMBER_TWO);
		return sysDictDao.addDictDetail(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.SysDictService#queryDictDetailListForPage(java.
	 * util.Map)
	 */
	@Override
	public List<DictDetailEntity> queryDictDetailListForPage(String dtlKey, String dtlValue, BigInteger dictId,
			Page page) {
		BigInteger bigInteger = dictId;
		List<DictDetailEntity> list = sysDictDao.queryDictDetailListForPage(dtlKey, dtlValue, dictId, page);
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.py.framework.service.SysDictService#fineOneDictDetail(java.lang.
	 * String)
	 */
	@Override
	public DictDetailEntity fineOneDictDetail(long id) {
		if (id > XypjConstants.NUMBER_ZERO) {
			return sysDictDao.fineOneDictDetail(id);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.SysDictService#batchUpdateDictDetailStatus(java.
	 * util.List, int, int)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int batchUpdateDictDetailStatus(List<Long> ids, int enabled, int delFlag) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!CollectionUtils.isEmpty(ids)) {
			params.put("ids", ids);
		}
		if (enabled > XypjConstants.NUMBER_ZERO) {
			params.put("enabled", enabled);
		}
		if (delFlag > XypjConstants.NUMBER_ZERO) {
			DictDetailEntity detail = null;
			for (Long id : ids) {
				// 判断是否使用中
				detail = this.fineOneDictDetail(id);
				if (null != detail && detail.getEnabled() == XypjConstants.NUMBER_TWO) {
					throw new ServiceException(detail.getDtlKey() + "正在启用中，删除失败！");
				}
			}
			params.put("delFlag", delFlag);
		}
		return sysDictDao.batchUpdateDictDetailStatus(params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.SysDictService#updateDictDetail(com.py.framework
	 * .entity.DictDetailEntity)
	 */
	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public int updateDictDetail(DictDetailEntity entity) {
		return sysDictDao.updateDictDetail(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.py.framework.service.SysDictService#getDictDetailByKey(long,
	 * long, java.lang.String)
	 */
	@Override
	public List<DictDetailEntity> getDictDetailByKey(long dictId, long dictDetailId, String dtlKey) {
		Map<String, Object> params = new HashMap<>();
		params.put("dictId", dictId);
		params.put("dictDetailId", dictDetailId);
		params.put("dtlKey", dtlKey);
		return sysDictDao.getDictDetailByKey(params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.py.framework.service.SysDictService#getDictDetails(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public List<DictDetailEntity> getDictDetails(String dictKey, String dtlKey) {
		if (StringUtils.isNotBlank(dictKey)) {
			List<DictDetailEntity> list = new ArrayList<>();
			list = sysDictDao.getDictDetails(dictKey, dtlKey);
			return list;
		}
		return null;
	}
	
	@Override
	public List<DictDetailEntity> getDictDetailsBylen(String dictKey, String dtlKey) {
		if (StringUtils.isNotBlank(dictKey)) {
			List<DictDetailEntity> list = new ArrayList<>();
			list = sysDictDao.getDictDetailsBylen(dictKey, dtlKey);
			return list;
		}
		return null;
	}

	@Override
	public List<LinkedHashMap> seletctDictInfo() {
		return sysDictDao.seletctDictInfo();
	}

	@Override
	public List<DictEntity> fineSameDict(String dictKey) {
		return sysDictDao.fineSameDict(dictKey);
	}
}